package com.example.aevovein.ui.skilltree;

import android.app.Application;
import android.os.Environment;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.aevovein.model.Skill;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SkilltreeViewModel extends AndroidViewModel {
    private final MutableLiveData<Map<String, List<Skill>>> skillsMap;
    private final Gson gson;

    public SkilltreeViewModel(Application application) {
        super(application);
        skillsMap = new MutableLiveData<>(new HashMap<>());
        gson = new Gson();
        loadSkills();
    }

    private void loadSkills() {
        File dataDir = new File(Environment.getExternalStorageDirectory(), "AevoVein/data");
        File skilltreeFile = new File(dataDir, "skilltree.json");
    
        try (FileReader reader = new FileReader(skilltreeFile)) {
            Type type = new TypeToken<Map<String, List<Skill>>>() {}.getType();
            Map<String, List<Skill>> skillMap = gson.fromJson(reader, type);
            skillsMap.setValue(skillMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public LiveData<Map<String, List<Skill>>> getSkillsMap() {
        return skillsMap;
    }
}


